#! /usr/bin/env fish

function git_is_repo --description 'Check if directory is a repository'
  test -d .git; or command git rev-parse --git-dir >/dev/null 2> /dev/null
end

function git_branch_name --description 'Get current branch name'
  git_is_repo; and begin
    command git symbolic-ref --short HEAD 2> /dev/null;
      or command git show-ref --head -s --abbrev | head -n1 2> /dev/null
  end
end

function git_is_touched --description 'Check if repo has any changes'
  git_is_repo; and begin
    # The first checks for staged changes, the second for unstaged ones.
    # We put them in this order because checking staged changes is *fast*.  
    not command git diff-index --cached --quiet HEAD -- >/dev/null 2>&1
    or not command git diff --no-ext-diff --quiet --exit-code >/dev/null 2>&1
  end
end

function git_is_stashed --description 'Check if repo has stashed contents'
  git_is_repo; and begin
    command git rev-parse --verify --quiet refs/stash >/dev/null
  end
end

function git_ahead --argument ahead behind diverged none
  not git_is_repo; and return

  set -l commit_count (command git rev-list --count --left-right "@{upstream}...HEAD" 2> /dev/null)

  switch "$commit_count"
  case ""
    # no upstream
  case "0"\t"0"
    test -n "$none"; and echo "$none"; or echo ""
  case "*"\t"0"
    test -n "$behind"; and echo "$behind"; or echo "-"
  case "0"\t"*"
    test -n "$ahead"; and echo "$ahead"; or echo "+"
  case "*"
    test -n "$diverged"; and echo "$diverged"; or echo "±"
  end
end
